package com.cyl.job;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import com.cyl.manager.aws.domain.entity.SystemStatistics;
import com.cyl.manager.aws.mapper.SystemStatisticsMapper;
import com.cyl.manager.aws.pojo.vo.ShopStatisticsSqlVO;
import com.cyl.manager.aws.service.ProfitDetailsStatisticsService;
import com.cyl.manager.aws.service.ShopStatisticsService;
import com.cyl.manager.aws.service.SystemStatisticsService;
import com.cyl.manager.yee.service.MerchantStatisticsService;
import com.cyl.manager.yee.service.YeeMerchantSifService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisService;
import com.ruoyi.system.service.ISysDeptService;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


@Component
@Slf4j
public class StatisticsJob {

    @Autowired
    private SystemStatisticsService systemStatisticsService;

    @Autowired
    private SystemStatisticsMapper systemStatisticsMapper;
    
    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private RedisService redisService;
    @Autowired
    private ShopStatisticsService shopStatisticsService;
    
    @Autowired
    private MerchantStatisticsService merchantStatisticsService;
    
    
    @Autowired
    private ProfitDetailsStatisticsService profitDetailsStatisticsService;
    
    @Value("${custom.synTaskEnable}")
	private boolean synTaskEnable;
//    @Async
//    @Scheduled(cron = "00 00 3 * * ?")
//    public void statDate() {
//        log.info("【统计昨日系统数据任务开始】");
//        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).plusDays(-1);
//    	String redisKey = "admin_statistics_statdate_123456";
//	    String redisValue = redisKey+"_"+startTime.toString();
//	    try{    
//			    redisService.lock(redisKey,redisValue,60*30);
//			    if(true) {
//				    	LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).plusDays(-1);
//				        SysDept sysDept = new SysDept();
//				        sysDept.setBmlx("5");
//				        sysDept.setDelFlag("0");
//				        List<SysDept> depts = deptService.selectDeptList2(sysDept);
//				        if(depts!=null) {
//				        	for (SysDept sd : depts) {
//				        		String[] as = sd.getAncestors().split(",");
//				        		Long deptZbjId = Long.parseLong(as[2]);
//				        		Long deptTzId = Long.parseLong(as[3]);
//				        		Long deptDpId = sd.getDeptId();
//				        		SystemStatistics data = systemStatisticsService.stat(deptZbjId, deptTzId, deptDpId, startTime, endTime);
//				        	    systemStatisticsMapper.insert(data);
//				        		
//							}
//				        }
//			    }
//	        log.info("【统计昨日系统数据任务结束】");
//	    	}catch (Exception e){
//	    		log.error("统计昨日系统数据任务异常",e);
//	    	}finally {
//		        try {
//	            redisService.unLock(redisKey,redisValue);
//	        }catch (Exception e){
//	            log.error("",e);
//	        }
//	    }
//    }
    
    
    
    
    @Async
    @Scheduled(cron = "00 00 1 * * ?")
    public void insertMerchantSifDay() {
    	if(!synTaskEnable) {
			return ;
		}
        log.info("【统计昨日系统结算数据任务开始】");
        LocalDateTime strTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).plusDays(-1);
    	LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).plusDays(-1);
    	String redisKey = "admin_yee_mer_sif_123456";
	    String redisValue = redisKey+"_"+strTime.toString();
	    try{    
			  redisService.lock(redisKey,redisValue,60*30);
			  merchantStatisticsService.insertMerchantStatisticsDay(strTime, endTime);
	        log.info("【统计昨日系统结算数据任务结束】");
	    	}catch (Exception e){
	    		log.error("统计昨日系统结算数据任务异常",e);
	    	}finally {
		        try {
	            redisService.unLock(redisKey,redisValue);
	        }catch (Exception e){
	            log.error("",e);
	        }
	    }
    }
    
    
    @Async
    @Scheduled(cron = "00 00 2 * * ?")
    public void insertProfitDay() {
    	if(!synTaskEnable) {
			return ;
		}
        log.info("【统计昨日系统各方利润数据任务开始】");
        LocalDateTime strTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).plusDays(-1);
    	LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).plusDays(-1);
    	String redisKey = "admin_profit_shop_123456";
	    String redisValue = redisKey+"_"+strTime.toString();
	    try{    
			  redisService.lock(redisKey,redisValue,60*30);
			  profitDetailsStatisticsService.insertProfitDay(strTime, endTime);
	        log.info("【统计昨日系统各方利润数据任务结束】");
    	}catch (Exception e){
    		log.error("统计昨日系统各方利润数据任务异常",e);
    	}finally {
		        try {
	            redisService.unLock(redisKey,redisValue);
	        }catch (Exception e){
	            log.error("",e);
	        }
	    }
    }


    @Async
    @Scheduled(cron = "00 00 3 * * ?")
    public void insertShopDay() {
    	if(!synTaskEnable) {
			return ;
		}
        log.info("【统计昨日系统数据任务开始】");
        LocalDateTime strTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).plusDays(-1);
    	LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).plusDays(-1);
    	String redisKey = "admin_statistics_shop_123456";
	    String redisValue = redisKey+"_"+strTime.toString();
	    try{    
			    redisService.lock(redisKey,redisValue,60*30);
			    if(true) {
			    	shopStatisticsService.insertShopDay(strTime, endTime);
			    }
	        log.info("【统计昨日系统数据任务结束】");
	    	}catch (Exception e){
	    		log.error("统计昨日系统数据任务异常",e);
	    	}finally {
		        try {
	            redisService.unLock(redisKey,redisValue);
	        }catch (Exception e){
	            log.error("",e);
	        }
	    }
    }
    

}
